package com.wiscamp.ninechapters.problems.domain.services;

import com.wiscamp.ninechapters.problems.application.dto.UserDashboardDto;
import com.wiscamp.ninechapters.problems.domain.gateway.UserGateway;
import cube.ddd.aspect.HandleServiceException;
import jakarta.validation.constraints.Min;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class DashboardService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TaskService taskService;

    @Autowired
    private SheetService sheetService;

    @Autowired
    private MistakeService mistakeService;

    @Autowired
    private UserGateway userGateway;

    /*@Autowired
    private StatisticsService statisticsService;*/

    /**
     * Gets user's dashboard data
     *
     * @param userId
     * @return
     */

    @HandleServiceException
    @Cacheable(value = "dashboard", key = "#userId")
    public UserDashboardDto getUserDashboard(@Min(0) long userId) {
        /*var dto = new UserDashboardDto();
        dto.setUserId(userId);

        dto.setTotalTaskQuantity(tasksService.getTaskQuantity(userId));
        dto.setUncompletedTaskQuantity(tasksService.getUncompletedTaskQuantity(userId));
        dto.setSheetQuantity(sheetsService.getSheetsQuantityByUser(userId));
        dto.setMistakeQuantity(mistakesService.getMistakesQuantityByUser(userId));

        *//*if (roleGateway.isItemBankAdmin(userId)) {
            dto.setMonthRegisteredUserQuantityForAdmin(userGateway.getRegistersQuantityByMonth());
            dto.setMonthSheetQuantityForAdmin(sheetsService.getSheetsQuantityByMonth());
            dto.setMonthTaskQuantityForAdmin(tasksService.getTasksQuantityByMonth());
        }*//*

        var now = LocalDateTime.now();
        // year
        Collection<StatisticsMenteePO> statisticsMenteeYearCollection = statisticsService.getStatisticsByMenteeYear(now.getYear(), userId);
        int yearPracticeQuantity = 0;
        double yearSeconds = 0;
        double[] yearMeanSecondsArray = new double[statisticsMenteeYearCollection.size()];
        double[] yearMeanDifficultyArray = new double[statisticsMenteeYearCollection.size()];
        double[] yearMeanRateArray = new double[statisticsMenteeYearCollection.size()];
        for (var sm : statisticsMenteeYearCollection) {
            int counter = 0;
            yearPracticeQuantity = yearPracticeQuantity + sm.getPraticeQuantity();
            yearSeconds = yearSeconds + sm.getSecondsSum();
            yearMeanSecondsArray[counter] = sm.getSecondsMean();
            yearMeanDifficultyArray[counter] = sm.getDifficultyMean();
            yearMeanRateArray[counter] = sm.getCorrectRate();
            counter++;
        }
        double yearMeanSeconds = StatisticsHelper.getMean(yearMeanSecondsArray);
        double yearMeanDifficulty = StatisticsHelper.getMean(yearMeanDifficultyArray);
        double yearMeanRate = StatisticsHelper.getMean(yearMeanRateArray);

        dto.setYearCorrectRateQuantity(Math.round(yearMeanRate * 10) / 10);
        dto.setYearMeanDifficulty(Math.round(yearMeanDifficulty * 10) / 10);
        dto.setYearMeanSeconds(Math.round(yearMeanSeconds * 10) / 10);
        dto.setYearPracticeQuantity(yearPracticeQuantity);
        dto.setYearSeconds(yearSeconds);


        // month
        Collection<StatisticsMenteePO> statisticsMenteeMonthCollection = statisticsService.getStatisticsByMenteeMonth(now.getYear(), now.getMonthValue(), userId);
        int monthPracticeQuantity = 0;
        double monthSeconds = 0;
        double[] monthMeanSecondsArray = new double[statisticsMenteeMonthCollection.size()];
        double[] monthMeanDifficultyArray = new double[statisticsMenteeMonthCollection.size()];
        double[] monthMeanRateArray = new double[statisticsMenteeMonthCollection.size()];
        for (var sm : statisticsMenteeMonthCollection) {
            int counter = 0;
            monthPracticeQuantity = monthPracticeQuantity + sm.getPraticeQuantity();
            monthSeconds = monthSeconds + sm.getSecondsSum();
            monthMeanSecondsArray[counter] = sm.getSecondsMean();
            monthMeanDifficultyArray[counter] = sm.getDifficultyMean();
            monthMeanRateArray[counter] = sm.getCorrectRate();
            counter++;
        }
        double monthMeanSeconds = StatisticsHelper.getMean(monthMeanSecondsArray);
        double monthMeanDifficulty = StatisticsHelper.getMean(monthMeanDifficultyArray);
        double monthMeanRate = StatisticsHelper.getMean(monthMeanRateArray);

        dto.setMonthCorrectRateQuantity(Math.round(monthMeanRate * 10) / 10);
        dto.setMonthMeanDifficulty(Math.round(monthMeanDifficulty * 10) / 10);
        dto.setMonthMeanSeconds(Math.round(monthMeanSeconds * 10) / 10);
        dto.setMonthPracticeQuantity(monthPracticeQuantity);
        dto.setMonthSeconds(monthSeconds);

        return dto;*/
        return null;
    }

}
